package com.yun2.bms.service.impl;

import com.yun2.bms.constant.SystemConstant;
import com.yun2.bms.dao.ILendDao;
import com.yun2.bms.dao.IUserDao;
import com.yun2.bms.dao.impl.LendDaoImpl;
import com.yun2.bms.dao.impl.UserDaoImpl;
import com.yun2.bms.model.User;
import com.yun2.bms.service.ILendService;
import com.yun2.bms.service.IUserService;

import java.util.List;

/**
 * author: youli
 * qq: 27855162@qq.com
 * date: 2020/6/4
 */

public class UserServiceImpl implements IUserService {

    private IUserDao userDao = new UserDaoImpl();

    private ILendDao lendDao = new LendDaoImpl();

    @Override
    public List<User> getAll() {
        List<User> list = this.userDao.getAll();
        for(User user : list){
            //角色信息
            if(user.getRole().equals(SystemConstant.Role.ROLE_ADMIN)){
                user.setRoleCn(SystemConstant.RoleCn.ROLE_ADMIN);
            }else if(user.getRole().equals(SystemConstant.Role.ROLE_TEACHER)){
                user.setRoleCn(SystemConstant.RoleCn.ROLE_TEACHER);
            }else{
                user.setRoleCn(SystemConstant.RoleCn.ROLE_STUDENT);
            }
            //性别信息
            if(user.getGender().equals(SystemConstant.Gender.GENDER_FEMALE)){
                user.setGenderCn(SystemConstant.GenderCn.GENDER_FEMALE);
            }else{
                user.setGenderCn(SystemConstant.GenderCn.ROLE_MALE);
            }
        }
        return list;
    }

    @Override
    public int delete(List<User> list) {
        String ids = "";
        //将list集合转换为字符串形式，27,27,28,29,29
        for (User user : list) {
            ids += user.getId();
            ids += ",";
        }
        ids = ids.substring(0, ids.length() - 1);

        return this.delete(ids);
    }

    /**
     * 删除多条记录
     *
     * @param ids:27,28,29
     * @return
     */
    @Override
    public int delete(String ids) {
        return this.userDao.delete(ids);
    }

    @Override
    public boolean isLoginNameUnique(String loginName) {
        return this.userDao.isLoginNameUnique(loginName);
    }

    @Override
    public boolean isLoginNameUnique(Integer id, String loginName) {
        return this.userDao.isLoginNameUnique(id, loginName);
    }

    @Override
    public int add(User user) {
        return this.userDao.add(user);
    }

    @Override
    public int edit(User user) {
        return this.userDao.edit(user);
    }

    @Override
    public List<User> getAllByUserInfo(String userInfo) {
        return this.userDao.getAllByUserInfo(userInfo);
    }

    @Override
    public User getUser4Lend(Integer id) {
        User user = this.userDao.get(id);
        //角色信息
        if(user.getRole().equals(SystemConstant.Role.ROLE_ADMIN)){
            user.setRoleCn(SystemConstant.RoleCn.ROLE_ADMIN);
        }else if(user.getRole().equals(SystemConstant.Role.ROLE_TEACHER)){
            user.setRoleCn(SystemConstant.RoleCn.ROLE_TEACHER);
        }else{
            user.setRoleCn(SystemConstant.RoleCn.ROLE_STUDENT);
        }
        //当前用户已经发生的借阅信息
        Integer myLendedNumber = this.lendDao.getLendedNumberByUserId(user.getId());
        user.setMyLendedNumber(myLendedNumber);

        return user;
    }

    @Override
    public User get(int userId) {
        return this.userDao.get(userId);
    }
}
